$(document).ready(function(){
	$('#product_code_form').submit(function(){
		return false;
	});
	$('#btn_add_product_code').click(function(){
		addproduct_code();
	});
	$('#btn_update_product_code').click(function(){
		updateproduct_code();
	});
	$('#btn_delete_product_code').click(function(){
		deleteproduct_code();
	});
	$('#btn_clear').click(function(){
		$('#product_code_code').val('');
		$('#product_code_description').val('');
		$('#product_code_hidden_id').val(0);
		$('#category_level_2').val(0);
		$('#category_level_3').val(0);
		$('#product_fit').val(0);
		$('#product_crotch').val(0);
		$('#product_code_id').val('');
		$('#submit_error').html('');
	});
	$('#btn_search_product_code').click(function(){
		getproduct_code();
	});
	$('#btn_search_code').click(function(){
		searchCategory();
	});
});

function searchCategory(){
	$('#submit_error').html('');
	var data = {
		keyword: $('#search_code').val()
	};
	$('#search_result').html('');
	var request = {
		url: base_url + SEARCH_PRODUCT_CODE,
		type: 'POST',
		data: data,
		success: function(data){
			if(data){
				$('#search_result').html(data);
				$('#product_code_table').dataTable({
					"bPaginate": true,
					"bLengthChange": false,
					"bFilter": false,
					"bSort": true,
					"bInfo": true,
					"bAutoWidth": false
				});
			} else {
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		},
		error: function(error){
			$('#submit_error').html(ERROR_ID_NOT_FOUND);
		}
	};
	excuteAjax(request);
}

function getproduct_code(){
	var product_code_id = parseInt($('#product_code_id').val());
	if(product_code_id){
		$('#submit_error').html('');
		var data = {
			id: product_code_id
		};
		var request = {
			url: base_url + GET_PRODUCT_CODE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data){
					data = JSON.parse(data);
					$('#category_level_2').val(data.category_level_2_id);
					$('#category_level_3').val(data.category_level_3_id);
					if(data.product_fit_id){
						$('#product_fit').val(data.product_fit_id);
					} else {
						$('#product_fit').val(0);
					}
					if(data.product_crotch_id){
						$('#product_crotch').val(data.product_crotch_id);
					} else {
						$('#product_crotch').val(0);
					}
					$('#product_code_code').val(data.code);
					$('#product_code_description').val(data.description);
					$('#product_code_hidden_id').val(data.id);
				} else {
					$('#submit_error').html(ERROR_ID_NOT_FOUND);
				}
			},
			error: function(error){
				$('#submit_error').html(ERROR_ID_NOT_FOUND);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INVALID_ID);
	}
}

function addproduct_code(){
	var category_level_2 = $('#category_level_2').val();
	var category_level_3 = $('#category_level_3').val();
	var product_fit = $('#product_fit').val();
	var product_crotch = $('#product_crotch').val();
	var product_code_code = $('#product_code_code').val();
	var product_code_description = $('#product_code_description').val();
	$('#submit_error').html('');
	if(product_code_code !== '' && category_level_2 != 0 && category_level_3 != 0){
		var data = {
			category_level_2_id: category_level_2,
			category_level_3_id: category_level_3,
			product_fit_id: product_fit,
			product_crotch_id: product_crotch,
			code: product_code_code,
			description: product_code_description
		};
		var request = {
			url: base_url + ADD_PRODUCT_CODE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function updateproduct_code(){
	var product_code_id = $('#product_code_hidden_id').val();
	var category_level_2 = $('#category_level_2').val();
	var category_level_3 = $('#category_level_3').val();
	var product_fit = $('#product_fit').val();
	var product_crotch = $('#product_crotch').val();
	var product_code_code = $('#product_code_code').val();
	var product_code_description = $('#product_code_description').val();
	$('#submit_error').html('');
	if(product_code_code !== '' && product_code_id != 0 && category_level_2 != 0 && category_level_3 != 0){
		var data = {
			id: product_code_id,
			category_level_2_id: category_level_2,
			category_level_3_id: category_level_3,
			product_fit_id: product_fit,
			product_crotch_id: product_crotch,
			code: product_code_code,
			description: product_code_description
		};
		var request = {
			url: base_url + UPDATE_PRODUCT_CODE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}

function deleteproduct_code(){
	var product_code_id = $('#product_code_hidden_id').val();
	$('#submit_error').html('');
	if(product_code_id != 0){
		var data = {
			id: product_code_id
		};
		var request = {
			url: base_url + DELETE_PRODUCT_CODE,
			type: 'POST',
			data: data,
			success: function(data){
				if(data == '1'){
					$('#submit_error').html(RESPONSE_SUCCESS);
				} else {
					$('#submit_error').html(RESPONSE_ERROR);
				}
			},
			error: function(error){
				$('#submit_error').html(RESPONSE_ERROR);
			}
		};
		excuteAjax(request);
	} else {
		$('#submit_error').html(ERROR_INPUT);
	}
	$("html, body").animate({ scrollTop: 0 }, "slow");
}